<?php
/**
 * This page saves edits to a container on the given instance of resourcepage,
 * then redirects back to view.
 * @copyright &copy; 2006 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package resourcepage
 *//** */

    require_once('module_resourcepage.php');
    
    $module=module_resourcepage::$inst;
 
    $id=required_param('id',PARAM_INT);    // Course Module ID
    $container=required_param('container',PARAM_INT);
    $title=stripslashes(required_param('title',PARAM_RAW));
    $scrollpos=optional_param('scrollpos',0,PARAM_INT);
    
    // add authids, livedate and removedate to sections - see bug #4039    
    $livedateenable=optional_param('livedateenable',null,PARAM_ALPHA);
    $livedateyear=optional_param('livedateyear',null,PARAM_INT);
    $livedatemonth=optional_param('livedatemonth',null,PARAM_INT);
    $livedateday=optional_param('livedateday',null,PARAM_INT);
    $removedateenable=optional_param('removedateenable',null,PARAM_ALPHA);
    $removedateyear=optional_param('removedateyear',null,PARAM_INT);
    $removedatemonth=optional_param('removedatemonth',null,PARAM_INT);
    $removedateday=optional_param('removedateday',null,PARAM_INT);
    $showto=optional_param('showto',null,PARAM_RAW);
    
    if($showto!==null) {
        $showto=strtoupper($showto);
        if(!preg_match('/^([A-Z0-9!]+( [A-Z0-9!]+)*)?$/',$showto)) {
            error('Unexpected parameter value for showto. Must be a space-separated list of authids.');
        }
    }
    
    
    
    // Basic security checks
    $module->obtain_view_info($id,null,$cm,$course,$resourcepage);
    $module->verify_edit_action($cm);

    // Just set the new title for now
    // Consider set_container_fields in module_resourcepage
    set_field('resourcepage_containers', 'title', addslashes($title), 'id', $container);
    
    // add authids, livedate and removedate to sections - see bug #4039
    
    if(isset($livedateenable)) {
        $livedate=
            round((make_timestamp($livedateyear, $livedatemonth, $livedateday)-
            $course->startdate) / (24*60*60)); 
    } else {
        $livedate=null;
    }
    
    set_field('resourcepage_containers', 'livedate',$livedate, 'id', $container);
    
    if(isset($removedateenable)) {
        $removedate=
            round((make_timestamp($removedateyear, $removedatemonth, $removedateday)-
            $course->startdate) / (24*60*60)); 
    } else {
        $removedate=null;
    }
    
    set_field('resourcepage_containers', 'removedate',$removedate, 'id', $container);

    if($showto !== null) {
    	set_field('resourcepage_containers', 'showto', addslashes($showto), 'id', $container);
    }
    
    
    // Log and return to view
    add_to_log($course->id, "resourcepage", "editcontainer", "view.php?id=$cm->id",
        "Edited #".$container,$cm->id);
    redirect('view.php?id='.$cm->id.
        ($scrollpos ? '&scrollpos='.$scrollpos : '')); 

?>